package com.sf.business.appointment.service.impl;

import com.sf.business.appointment.domain.BusAppointment;
import com.sf.business.appointment.domain.BusCheckout;
import com.sf.business.appointment.mapper.BusAppointmentMapper;
import com.sf.business.appointment.mapper.BusCheckoutMapper;
import com.sf.business.appointment.qo.BusCheckoutQO;
import com.sf.business.appointment.service.IBusCheckoutService;
import com.sf.business.appointment.util.AssertArgumentsUtil;
import com.sf.business.appointment.vo.BusCheckoutVO;
import com.sf.common.utils.DateUtils;
import com.sf.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 结算单Service业务层处理
 * 
 * @author liuhe
 * @date 2025-03-23
 */
@Service
public class BusCheckoutServiceImpl implements IBusCheckoutService 
{
    @Autowired
    private BusCheckoutMapper busCheckoutMapper;
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;

    /**
     * 查询结算单
     * 
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public BusCheckout selectBusCheckoutById(Long id)
    {
        return busCheckoutMapper.selectBusCheckoutById(id);
    }

    /**
     * 查询结算单列表
     * 
     * @return 结算单
     */
    @Override
    public List<BusCheckout> selectBusCheckoutList(BusCheckoutQO vo)
    {
        return busCheckoutMapper.selectBusCheckoutList(vo);
    }

    /**
     * 新增结算单
     * 
     * @return 结果
     */
    @Override
    public int insertBusCheckout(BusCheckoutVO vo)
    {
        Long appointmentId = vo.getAppointmentId();
        BusCheckout busCheckout = new BusCheckout();

        if (appointmentId != null) {
            BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(appointmentId);
            if (busAppointment == null)
                throw new IllegalArgumentException("预约单不存在");
            if (busAppointment.getStatus() != BusAppointment.STATUS_ARRIVAL)
                throw new IllegalArgumentException("预约单状态不对 , 需要 [ 已到店 ]");
            busAppointment.setStatus(BusAppointment.STATUS_SETTLEMENT_GENERATED);

            BeanUtils.copyProperties(busAppointment, busCheckout);
        } else {
            AssertArgumentsUtil.assertArguments(vo);
            BeanUtils.copyProperties(vo, busCheckout);
        }


        busCheckout.setStatus(BusCheckout.STATUS_CONSUMING);
        busCheckout.setActualArrivalTime(new Date());
        busCheckout.setCreateTime(DateUtils.getNowDate());
        return busCheckoutMapper.insertBusCheckout(busCheckout);
    }

    /**
     * 修改结算单
     * 
     * @return 结果
     */
    @Override
    public int updateBusCheckout(BusCheckoutVO vo)
    {
        AssertArgumentsUtil.assertArguments(vo);

        BusCheckout busCheckout = busCheckoutMapper.selectBusCheckoutById(vo.getId());
        if (busCheckout == null)
            throw new IllegalArgumentException("数据不存在");

        BeanUtils.copyProperties(vo, busCheckout);

        busCheckout.setUpdateTime(DateUtils.getNowDate());

        return busCheckoutMapper.updateBusCheckout(busCheckout);
    }

    /**
     * 批量删除结算单
     * 
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusCheckoutByIds(Long[] ids)
    {
        return busCheckoutMapper.deleteBusCheckoutByIds(ids);
    }

    /**
     * 删除结算单信息
     * 
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBusCheckoutById(Long id)
    {
        BusCheckout busCheckout = busCheckoutMapper.selectBusCheckoutById(id);
        if (busCheckout == null)
            throw new IllegalArgumentException("数据不存在");
        if (busCheckout.getStatus() == BusCheckout.STATUS_PAYED)
            throw new IllegalArgumentException("结算单已结算，不能删除");
        busCheckout.setIsDelete(1);

        // 预约界面生成的结算单, 删除结算单则将预约状态改为已到店 (可以重新生成结算单)
        if (busCheckout.getAppointmentId() != null){
            BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(busCheckout.getAppointmentId());
            busAppointment.setStatus(BusAppointment.STATUS_ARRIVAL);
            busAppointmentMapper.updateBusAppointment(busAppointment);
        }

        return busCheckoutMapper.updateBusCheckout(busCheckout);
    }

    @Override
    @Transactional
    public int pay(Long id) {
            if (id == null)
            throw new IllegalArgumentException("数据不存在");
        BusCheckout busCheckout = busCheckoutMapper.selectBusCheckoutById(id);
        if (busCheckout == null)
            throw new IllegalArgumentException("数据不存在");
        if (busCheckout.getStatus() == BusCheckout.STATUS_PAYED)
            throw new IllegalStateException("结算单已结算，不能修改 (必须消费中状态才行");

        // 修改收款人id为当前系统登录用户的id,  收款时间, 和状态改为已结算
        busCheckout.setPayeeId(SecurityUtils.getUserId());

        if (busCheckout.getPayTime() == null) {
            busCheckout.setPayTime(DateUtils.getNowDate());
            busCheckout.setStatus(BusCheckout.STATUS_PAYED);
        }
        busCheckoutMapper.updateBusCheckout(busCheckout);

        // 通过预约生成的结算单需要额外修改预约表
        Long appointmentId = busCheckout.getAppointmentId();
        if (appointmentId != null) {
            BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(appointmentId);
            if (busAppointment == null)
                throw new IllegalArgumentException("预约单不存在");
            if (busAppointment.getStatus() != BusAppointment.STATUS_SETTLEMENT_GENERATED)
                throw new IllegalStateException("预约单状态不对 , 需要 [ 结算单已生成 ]");
            busAppointment.setStatus(BusAppointment.STATUS_PAYMENT_GENERATED);
            busAppointmentMapper.updateBusAppointment(busAppointment);
        }
        return 0;
    }
}
